package persentation.presentTeacher;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;

import javax.swing.*;
import javax.swing.border.BevelBorder;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.table.DefaultTableModel;








import vo.courseVO.CourseVO;
import businesslogic.courseBl.CourseController;

public class ShowCoursePanel extends JPanel{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private ArrayList<CourseVO> courseVOs;
	private JTable table;
	private JButton showButton;
	private JScrollPane scrollPane;
	private MyTabbedPane tabbedPane;
	/**
	 * Create the panel.
	 */
	
	public ShowCoursePanel(CourseController courseController) {

		try {
			courseVOs=courseController.getMyCourse();
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
			ExceptionHandler.networkException();
		}
		setSize(800, 600);
		setLayout(null);
		
		scrollPane = new JScrollPane();
		scrollPane.setBounds(10, 10, 780, 404);
		this.add(scrollPane);
		
	    
		showButton=new JButton("查看详细信息");
		showButton.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				CourseVO courseVO=courseVOs.get(table.getSelectedRow());
				
				System.out.println(table.getSelectedRow());
				System.out.println(courseVO.getCourseName());
				if(tabbedPane.indexOfTab(courseVO.getCourseName()+" 详细信息")!=-1){
					tabbedPane.removeOne(tabbedPane.indexOfTab(courseVO.getCourseName()+" 详细信息"));
					tabbedPane.remove(tabbedPane.indexOfTab(courseVO.getCourseName()+" 详细信息"));

					tabbedPane.updateUI();

				}

				System.out.println("+++++++++++++++++++++++++");
				System.out.println(tabbedPane==null);
				System.out.println(new CourseInfoPanel(courseVO)==null);
				System.out.println(courseVO.getCourseName()+" 详细信息"==null);

				
				tabbedPane.addOne(courseVO.getCourseName()+" 详细信息",new CourseInfoPanel(courseVO));
			
			
				
				table.clearSelection() ;
				
				showButton.setEnabled(false);
			
			}
		});
		showButton.setBounds(300, 452, 174, 47);
		showButton.setEnabled(false);
		showButton.setFont(new Font("微软雅黑", Font.PLAIN, 20));
		add(showButton);
		
		
		
		
		table = new JTable();
	
		table.setFont(new Font("微软雅黑", Font.PLAIN, 13));
		table.setModel(new DefaultTableModel(
			new Object[][] {
				
				
				
				
			},
			new String[] {
				"课程编号", "课程名称", "上课院系", "上课时间", "学分"
			}
		) {
			/**
			 * 
			 */
			private static final long serialVersionUID = 1L;
			@SuppressWarnings("rawtypes")
			Class[] columnTypes = new Class[] {
				String.class, String.class, String.class, String.class, String.class
			};
			@SuppressWarnings({ "unchecked", "rawtypes" })
			public Class getColumnClass(int columnIndex) {
				return columnTypes[columnIndex];
			}
		});
		table.setBounds(119, 424, 262, 143);
		scrollPane.setViewportView(table);
		table.setForeground(new Color(0, 0, 0));
		table.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null));
	
		
		table.addMouseListener(new MouseListener() {
			
			@Override
			public void mouseReleased(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void mousePressed(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void mouseExited(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void mouseEntered(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void mouseClicked(MouseEvent e) {
				if(table.getSelectedRow()!=-1)
					showButton.setEnabled(true);
				
			}
		});
		
	
		
		
		table.add(table.getTableHeader());
		table.getTableHeader().setReorderingAllowed(false);
		table.getTableHeader().setResizingAllowed(false);
		
		
		fillTable();

	}
	
	public void fillTable(){
		DefaultTableModel tableModel = (DefaultTableModel) table.getModel();
		 tableModel.setRowCount(0);
		 for(CourseVO course:courseVOs){
			 String[] array=new String [5];
			 array[0]=course.getCourseId();
			 array[1]=course.getCourseName();
	 		 array[2]=course.getDepartmentID();
			 array[3]=course.getCourseTime();
			 array[4]=String.valueOf(course.getCredit());
			 
			 tableModel.addRow(array);
			 table.invalidate();
			 table.updateUI();
	      }
	} 
	
	
	public void setTabbedPane(MyTabbedPane tabbedPane){
		this.tabbedPane=tabbedPane;
	}

	
}
